<template>
  <el-dialog v-model="dialogTableVisible4" :show-close="true" width="60%">
    <div style="height: 465px; overflow: hidden">
      <div style="position: absolute; top: 5px">
        <el-input
          style="width: 30%; margin-left: 2px"
          v-model="inputK3Bm"
          placeholder="k3编码"
        >
          <template #prefix>
            <el-icon class=""><search /></el-icon>
          </template>
        </el-input>
        <el-input
          style="width: 30%; margin-left: 2px"
          v-model="inputK3Mc"
          placeholder="k3名称"
        >
          <template #prefix>
            <el-icon class=""><search /></el-icon>
          </template>
        </el-input>
        <el-button style="margin-left: 5px" @click="searc3Kh" type="primary"
          >查询</el-button
        >
        <el-button style="margin-left: 5px" @click="put3Kh" type="primary"
          >添加</el-button
        >
        <el-button style="margin-left: 5px" @click="updateK3" type="success"
          >更新k3</el-button
        >
      </div>
      <div class="dialogDiv">
        <el-table
          border
          stripe
          :data="tableData3"
          @row-click="RowClick3"
          :cell-style="cellStyle"
          :header-cell-style="headerCellStyle"
          max-height="420px"
          height="420px"
          @selection-change="handleSelectionChange1"
          show-overflow-tooltip
        >
          <el-table-column type="selection" width="65" align="center" />
          <el-table-column prop="k3bm" label="Id" width="80" align="center" />
          <el-table-column prop="fnumber" label="k3编码" width="120" align="center" />
          <el-table-column prop="bmmc" label="k3名称" width="150" align="center" />
          <el-table-column prop="wlgg" label="物料规格" width="150" align="center" />
          <el-table-column prop="jldw" label="计量单位" width="60" align="center" />
        </el-table>

        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="page2"
            v-model:page-size="limit2"
            :page-sizes="[100, 200, 300, 400]"
            :small="small"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total3"
            @size-change="handleSizeChange3"
            @current-change="handleCurrentChange3"
          />
        </div>
      </div>
    </div>
  </el-dialog>
  <el-dialog
    v-model="dialogTableVisible2"
    :show-close="true"
    width="60%"
    style="z-index: 100"
  >
    <div style="height: 500px; overflow: hidden">
      <div style="top: 5px">
        <el-input style="width: 150px; margin-left: 2px" v-model="ID" placeholder="id">
          <template #prefix>
            <el-icon class=""><search /></el-icon>
          </template>
        </el-input>
        <el-input
          style="width: 150px; margin-left: 2px"
          v-model="tableName"
          placeholder="表名"
        >
          <template #prefix>
            <el-icon class=""><search /></el-icon>
          </template>
        </el-input>

        <el-button style="margin-left: 5px" @click="geSysStatus" type="primary"
          >查询</el-button
        >
      </div>
      <div class="dialogDiv">
        <el-table
          border
          stripe
          :data="tableData2"
          @row-click="RowClick2"
          :cell-style="cellStyle"
          :header-cell-style="headerCellStyle"
          max-height="420px"
          height="420px"
          @selection-change="handleSelectionChange2"
          show-overflow-tooltip
        >
          <el-table-column fixed type="index" width="60" align="center" />
          <el-table-column fixed type="selection" width="50" align="center" />
          <!-- <el-table-column
            prop="equipmentId"
            label="设备编号"
            width="100"
            align="center"
          />
          <el-table-column
            prop="equipmentName"
            label="设备名称"
            width="100"
            align="center"
          /> -->
          <el-table-column
            prop="mechanicalStructureId"
            label="结构编号"
            width="100"
            align="center"
          />
          <el-table-column
            prop="structureName"
            label="结构名称"
            width="100"
            align="center"
          />
          <el-table-column
            prop="structureID"
            label="位置编号"
            width="100"
            align="center"
          />
          <el-table-column prop="model" label="型号" width="100" align="center" />
          <el-table-column prop="quantity" label="数量" width="100" align="center" />
          <el-table-column prop="direction" label="说明" align="center" />
        </el-table>
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="page2"
            v-model:page-size="limit2"
            :page-sizes="[100, 200, 300, 400]"
            :small="small"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total2"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"
          />
        </div>
      </div>
    </div>
  </el-dialog>
  <div
    v-if="dialogTableVisible1"
    :fullscreen="false"
    :modal="false"
    :show-close="false"
    :width="screenWidth + 20 + 'px'"
    :append-to-body="true"
    style="
      position: absolute;
      left: 6rem;
      margin-top: 42px;
      z-index: 10;
      background-color: white;
    "
    :close-on-click-modal="false"
  >
    <div :style="{ width: screenWidth - 96 + 'px' }">
      <div
        :style="{
          height: '40px',
          borderBottom: '1px solid #ccc',
          width: '100%',
        }"
      >
        <div style="position: absolute; right: 15px">
          <el-button @click="flushed" type="primary"> 刷新</el-button>
          <!-- <el-button v-if="Permission" @click="upButton" type="primary">
        修改业务员</el-button
      > -->
          <!-- <el-button @click="addRow" type="primary">增行</el-button>
          <el-button @click="deleteSelectedRows" type="danger">删除</el-button> -->
          <el-button
            type="primary"
            @click="insertMaterialsFixed()"
            v-if="showInsertButton"
            >保存</el-button
          >
          <el-button
            type="primary"
            @click="updateMaterialsFixed()"
            v-if="!showInsertButton"
            >修改</el-button
          >

          <el-button @click="OFFdialogTableVisible" type="danger">关闭</el-button>
        </div>
      </div>
    </div>
    <div
      ref="titleDiv"
      :style="{
        backgroundColor: 'rgba(247, 247, 247, 0.903)',
        height: childTableHeight - 89 + 'px',
        width: screenWidth - 109 + 'px',
      }"
    >
      <el-form ref="ruleFormRef" status-icon :rules="rules" :model="form" :inline="true">
        <el-row :gutter="0">
          <!-- <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>设备编号</label
            >
            <el-input
              style="width: 200px"
              :readonly="true"
              v-model="form.equipmentId"
              placeholder=""
              clearable
              disabled
            />
          </el-form-item> -->
          <!-- 
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              >设备结构编号</label
            >
            <el-input
              style="width: 200px"
              @click="openBusiness(index)"
              :readonly="true"
              v-model="form.equipmentStructureId"
              placeholder="请选择"
            />
          </el-form-item> -->
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>单件唯一码</label
            >
            <el-input style="width: 200px" v-model="form.partSingleId" placeholder="" />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>供应商唯一码</label
            >
            <el-input
              style="width: 200px"
              v-model="form.supplierUniqueCode"
              placeholder=""
              clearable
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>物料编号</label
            >
            <el-input
              @click="openWlbm()"
              style="width: 200px"
              v-model="form.wlbm"
              placeholder=""
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">登记类型</label>
            <el-select
              style="width: 200px"
              clearable
              v-model="form.inputType"
              :readonly="true"
              @change="inputTypeChange()"
            >
              <el-option label="领用登记" value="0"></el-option>
              <el-option label="期初登记" value="1"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">物料状态</label>
            <el-select
              style="width: 200px"
              clearable
              v-model="form.materialStatus"
              :readonly="true"
              :disabled="inputTypeTrue"
            >
              <el-option label="初始状态" value="0"></el-option>
              <el-option label="流通中" value="1"></el-option>
              <el-option label="搁置" value="2"></el-option>
              <el-option label="报废" value="3"></el-option>
              <!-- <el-option label="异常" value="4"></el-option> -->
            </el-select>
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">流转状态</label>
            <el-select
              style="width: 200px"
              clearable
              v-model="form.circulationState"
              :readonly="true"
              :disabled="inputTypeTrue"
            >
              <el-option label="初始状态" value="0"></el-option>
              <el-option label="送回/领回" value="1"></el-option>
              <el-option label="安装" value="2"></el-option>
              <el-option label="拆卸" value="3"></el-option>
              <el-option label="送磨" value="4"></el-option>
              <el-option label="磨削" value="5"></el-option>
              <el-option label="搁置" value="10"></el-option>
              <el-option label="报废" value="11"></el-option>
              <el-option label="维修" value="12"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">锁定</label>
            <el-select
              style="width: 200px"
              clearable
              v-model="form.lock"
              :readonly="true"
              disabled
            >
              <el-option label="未锁定" value="0"></el-option>
              <el-option label="锁定" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">单据时间</label>
            <el-input
              style="width: 200px"
              :readonly="true"
              v-model="form.djDate"
              disabled
              placeholder="系统自动生成"
            />
          </el-form-item>
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>累计磨削次数</label
            >
            <el-input
              style="width: 200px"
              v-model="form.ljgrindingTimes"
              placeholder=""
              clearable
              @blur="() => handleBlur('ljgrindingTimes', index)"
              :disabled="!gunXiang || !daoZhou"
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>剩余磨削量</label
            >
            <el-input
              style="width: 200px"
              v-model="form.residualGrindingCapacity"
              placeholder=""
              clearable
              @blur="() => handleBlur('residualGrindingCapacity', index)"
              :disabled="!gunXiang || !daoZhou"
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              ><span style="color: red">*</span>累计磨削数量</label
            >
            <el-input
              style="width: 200px"
              v-model="form.ljgrindingNumber"
              placeholder=""
              clearable
              @blur="() => handleBlur('ljgrindingNumber', index)"
              :disabled="!gunXiang || !daoZhou"
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">型号</label>
            <el-input
              style="width: 200px"
              v-model="form.modelName"
              :readonly="true"
              placeholder="请选择"
              @click="openBusiness(index)"
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">颜色</label>
            <el-input
              style="width: 200px"
              v-model="form.color"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">零件编号</label>
            <el-input
              style="width: 200px"
              :readonly="true"
              v-model="form.businessProjectId"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">磨削总量</label>
            <el-input
              style="width: 200px"
              v-model="form.totalGrindingAmount"
              placeholder=""
              :readonly="true"
              @blur="() => handleBlur('totalGrindingAmount', index)"
              disabled
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">粗糙度</label>
            <el-input
              style="width: 200px"
              v-model="form.roughnessStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">材质</label>
            <el-input
              style="width: 200px"
              v-model="form.material"
              placeholder=""
              :readonly="true"
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">数量</label>
            <el-input
              style="width: 200px"
              v-model="form.quantity"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">耗材类别</label>
            <!-- <el-select
                v-model="categoryOfConsumables"
                size="medium"
                style="width: 200px"
                clearable
                @change="clickOrganizationStatus()"
              >
                <el-option
                  v-for="item in optionss"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                /> -->
            <el-input
              style="width: 200px"
              :readonly="true"
              v-model="form.categoryOfConsumables"
              placeholder=""
              disabled
              @change="clickOrganizationStatus()"
            />
            <!-- </el-select> -->
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">规格</label>
            <el-input
              style="width: 200px"
              v-model="form.specificationName"
              :readonly="true"
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">工作面长度</label>
            <el-input
              style="width: 200px"
              v-model="form.workingFaceLength"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              >是否允许人工报废</label
            >
            <el-input
              style="width: 200px"
              v-model="form.allowManualScrap"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">关闭状态</label>
            <el-input
              style="width: 200px"
              v-model="form.closeStatus"
              placeholder="默认状态"
              disabled
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">重量</label>
            <el-input
              style="width: 200px"
              v-model="form.weight"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">长度</label>
            <el-input
              style="width: 200px"
              v-model="form.length"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">宽度</label>
            <el-input
              style="width: 200px"
              v-model="form.width"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">直径</label>
            <el-input
              style="width: 200px"
              v-model="form.diameter"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">高度</label>
            <el-input
              style="width: 200px"
              v-model="form.height"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">锥度</label>
            <el-input
              style="width: 200px"
              v-model="form.taperStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">锥长</label>
            <el-input
              style="width: 200px"
              v-model="form.taperLength"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">锥高</label>
            <el-input
              style="width: 200px"
              v-model="form.taperHeight"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">内孔标准</label>
            <el-input
              style="width: 200px"
              v-model="form.innerHoleStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">硬度标准</label>
            <el-input
              style="width: 200px"
              v-model="form.hardnessStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              >工作面外径标准</label
            >
            <el-input
              style="width: 200px"
              v-model="form.workingFaceOuterDiameterStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px">厚度标准</label>
            <el-input
              style="width: 200px"
              v-model="form.thicknessStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>
        </el-row>
        <el-row :gutter="0">
          <el-form-item class="labs" style="margin-right: 0">
            <label class="lab" style="width: 120px; padding-right: 5px"
              >非工作面外径标准</label
            >
            <el-input
              style="width: 200px"
              v-model="form.nonWorkingFaceOuterDiameterStandard"
              :readonly="true"
              placeholder=""
              disabled
            />
          </el-form-item>

          <!-- <el-col :span="6">
            <el-form-item class="labs" style="margin-right: 0">
              <label class="lab" style="width: 120px; padding-right: 5px"
                ><span style="color: red">*</span>控制方式</label
              >
              <el-input
                style="width: 200px"
                v-model="form.ljgrindingNumber"
                placeholder=""
                clearable
              />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
    </div>
  </div>
  <el-container>
    <el-header height="65px" :style="{ 'border-bottom': '1px solid #ccc' }">
      <!-- 头部按钮 -->
      <el-row type="flex" justify="end" style="background-color: rgb(211, 227, 253)">
        <div>
          <el-button @click="searchButton" type="primary"> 查询</el-button>
          <el-button @click="handleAudit" type="warning"> 审核</el-button>
          <el-button @click="handleAudit0" type="warning"> 弃审</el-button>
          <el-button @click="openInseart" type="success"> 新增</el-button>
          <el-button @click="deleteButton" type="danger"> 删除</el-button>
        </div>
        <!-- <div class="title">需求汇总统计表</div> -->
      </el-row>
      <div
        style="
          margin-left: 6rem;
          height: 30px;

          background: rgb(241, 244, 243);
          margin-bottom: 0;
          position: relative;
          display: flex;
          justify-content: flex-start;
        "
      >
        <el-input style="width: 10%; margin-left: 2px" v-model="id" placeholder="id">
          <template #prefix>
            <el-icon class="">
              <search />
            </el-icon>
          </template>
        </el-input>
        <!-- <el-input
          style="width: 10%;  margin-left: 2px; "
          v-model="inputCode"
          placeholder="班组编号"
        >
          <template #prefix>
            <el-icon class="">
              <search />
            </el-icon>
          </template>
        </el-input> -->
      </div>
    </el-header>
    <el-container>
      <el-container>
        <el-main style="padding: 2px 2px 0 2px; margin-left: 6rem">
          <el-table
            v-loading="loading"
            ref="table"
            :data="tableData"
            border
            stripe
            :cell-style="cellStyle"
            :header-cell-style="headerCellStyle"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleRowClick"
            @row-click="RowClick"
            :row-style="rowState"
            :row-key="(row: any) => row.ID"
            :max-height="screenHeight"
            :height="screenHeight"
            :max-width="'300px'"
          >
            <el-table-column fixed type="index" width="60" align="center" />
            <el-table-column fixed type="selection" width="50" align="center" />
            <el-table-column
              prop="djNumber"
              label="单据编号"
              width="100"
              align="center"
            />
            <el-table-column
              prop="categoryOfConsumables"
              label="耗材类别"
              width="100"
              align="center"
            />
            <el-table-column
              prop="equipmentStructureId"
              label="设备结构编号"
              width="100"
              align="center"
            />
            <el-table-column
              prop="structureName"
              label="结构名称"
              width="100"
              align="center"
            />
            <!-- <el-table-column
              prop="equipmentId"
              label="设备编号"
              width="100"
              align="center"
            />
            <el-table-column
              prop="equipmentName"
              label="设备名称"
              width="100"
              align="center"
            /> -->
            <el-table-column
              prop="businessProjectId"
              label="零件编号"
              width="100"
              align="center"
            />
            <el-table-column prop="name" label="零件名称" width="200" align="center" />
            <el-table-column prop="model" label="型号" width="200" align="center" />
            <el-table-column
              prop="workingFaceOuterDiameterStandard"
              label="工作面外径标准"
              width="200"
              align="center"
            />
            <el-table-column prop="djDate" label="单据日期" width="200" align="center" />
            <el-table-column
              prop="djStatus"
              label="单据状态"
              width="100"
              align="center"
            />
          </el-table>
        </el-main>
        <el-footer style="height: 20px">
          <div class="demo-pagination-block">
            <el-pagination
              v-model:current-page="page"
              v-model:page-size="limit"
              :page-sizes="[50, 500, 1000, 1500, 2000, 8000]"
              :small="true"
              :background="true"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script setup lang="ts">
import {
  ref,
  watch,
  reactive,
  onMounted,
  onBeforeMount,
  computed,
  nextTick,
  onUnmounted,
  toRefs,
} from "vue";
import { Delete, Edit, Search, Share, Upload, Calendar } from "@element-plus/icons-vue";
import {
  ElButton,
  ElDialog,
  dialogEmits,
  ElMessage,
  ElTable,
  TableColumnCtx,
  ElMessageBox,
} from "element-plus";
import type { FormInstance, FormRules } from "element-plus";

import { CircleCloseFilled } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import axios from "../../axios/axiosInstance.ts";
import Cookies from "js-cookie";
import { format } from "date-fns";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
// crud封装组件
import * as crud from "../../components/utils/crud.ts";
import SearchTableDialog from "@/components/dialogs/SearchTableDialog.vue";
import * as notifications from "../../components/utils/notification.ts";
// 时间
// 获取当前时间

const page1 = ref(1);
const page2 = ref(1);
const page3 = ref(1);
const page4 = ref(1);
const page5 = ref(1);

const dialogTableVisible2 = ref(false);
const dialogTableVisible3 = ref(false);
const dialogTableVisible4 = ref(false);
const dialogTableVisible5 = ref(false);
const limit1 = ref(50);
const limit2 = ref(50);
const limit3 = ref(50);
const limit4 = ref(50);
const limit5 = ref(50);
const total1 = ref(10);
const total2 = ref(10);
const total3 = ref(10);
const total4 = ref(10);
const total5 = ref(10);
const categoryOfConsumables = ref("");
const djStatus = ref(0);
const direction = ref("");
const wlbm = ref("");
const ID = ref("");

const getCurrentFormattedDateTime = () => {
  const currentDateTime = new Date();

  // 格式化当前时间
  const formattedDateTime = currentDateTime.toLocaleString("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
  });
  // const formattedDateTime = format(currentDateTime, "YYYY-MM-dd HH:mm:ss");

  return formattedDateTime;
};
// 获取年月日
const getCurrentFormattedDate = () => {
  const currentDateTime = new Date();

  // 格式化当前时间，只获取年月日
  const formattedDate = currentDateTime.toLocaleString("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  });

  return formattedDate;
};
// 权限控制
const searchButtonPermissionsA = ref(false);
const menuId = ref(0);
const buttonVariables = {
  searchButtonPermissionsA,
};
const selectPermissionsButton = () => {
  const userName = Cookies.get("userName");
  axios({
    url: "/select/Permissions/Button",
    method: "post",
    data: {
      userName: userName,
      menuId: menuId.value,
    },
  }).then((res) => {
    res.data.forEach((button) => {
      const buttonName = button.buttonName;
      if (buttonVariables.hasOwnProperty(buttonName)) {
        buttonVariables[buttonName].value = true;
      }
    });
  });
};
selectPermissionsButton();
//权限结束
const tableData = ref([]);
const dialogTableVisible1 = ref(false);
const loading = ref(false);
const page = ref(1);
const limit = ref(50);
const total = ref(50);
const id = ref("");
const inputName = ref("");
const inputCode = ref("");
const selectBusinessDate = computed(() => ({
  page: page.value,
  limit: limit.value,
  id: id.value,
  //   inputCode: inputCode.value,
}));
const handleSizeChange = (val: number) => {
  selectBusinessType();
};
const handleCurrentChange = (val: number) => {
  selectBusinessType();
};
const searchButton = () => {
  selectBusinessType();
};
const selectBusinessType = async () => {
  loading.value = true;
  try {
    const res = await crud.fetchData(
      "/select/SingleRegistrationTable",
      "post",
      selectBusinessDate.value
    );
    if (res.length == 0) {
      total.value = 0;
      tableData.value = [];
      loading.value = false;
      return;
    }
    const definitionsList = res;
    total.value = definitionsList[0].total;
    loading.value = false;
    tableData.value = definitionsList.map((item: any) => ({
      autoId: item.autoId,
      id: item.id,
      djNumber: item.djNumber,
      categoryOfConsumables:
        item.categoryOfConsumables == 0
          ? "辊轴类"
          : item.categoryOfConsumables == 1
          ? "刀具类"
          : item.categoryOfConsumables == 2
          ? "隔套类"
          : item.categoryOfConsumables == 3
          ? "辊箱"
          : item.categoryOfConsumables == 4
          ? "刀轴"
          : "其他",
      closeStatus: item.closeStatus,
      equipmentStructureId: item.equipmentStructureId,
      equipmentId: item.equipmentId,
      businessProjectId: item.businessProjectId,
      djDate: item.djDate,
      maxWorkingFaceOuterDiameter: item.maxWorkingFaceOuterDiameter,
      minWorkingFaceOuterDiameter: item.minWorkingFaceOuterDiameter,
      workingFaceOuterDiameterStandard: item.workingFaceOuterDiameterStandard,
      name: item.name,
      structureName: item.structureName,
      equipmentName: item.equipmentName,
      model: item.model,
      djStatus: item.djStatus == 0 ? "未审核" : "已审核",
    }));
  } catch (error) {
    loading.value = false;
    console.error("请求失败:", error);
  }
};
selectBusinessType();
// 表格
const headerCellStyle = {
  // E9E9E7
  borderColor: "#B9C9C7",
  backgroundColor: "#F2F4FA", // 表头背景颜色
  fontWeight: "bold", // 表头文字粗细
  color: "#1F2129",
  // padding: "0 0 0 30px",
};
const rowState = (arg) => {
  return {
    height: "20px", // 设置合适的行高值
    fontSize: "12px", // 设置合适的字体大小值
    // color: "black",
  };
};
const handleBlur = (fieldName, rowIndex) => {
  const val = form[fieldName];
  validateAndFormat(fieldName, val);
};
const validateAndFormat = (prop, val) => {
  if (!isNaN(parseFloat(val)) && isFinite(val)) {
    // 将数字保留四位小数
    form[prop] = parseFloat(val).toFixed(4);
  } else {
    // 如果不是有效数字，清空表单
    form[prop] = "";
  }
};
const RowClick = (row, selection) => {
  table.value.toggleRowSelection(row);
};
const selectedRows = ref([]);
const table = ref();

const cellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (selectedRows.value.find((selectedRow) => selectedRow.ID === row.ID)) {
    return {
      backgroundColor: "#DFF0DB !important",
      borderColor: "#B9C9C7",
      padding: "4px",
    };
  } else {
    return { borderColor: "#B9C9C7", padding: "4px", margin: 0 };
  }
};
const handleSelectionChange = (selection: any) => {
  selectedRows.value = selection;
};
const screenHeight = ref(null);
const screenWidth = ref(null);
const childTableHeight = ref(null);

const getScreenHeight = () => {
  childTableHeight.value = window.innerHeight;
  screenHeight.value = window.innerHeight - 146;
  screenWidth.value = window.innerWidth;
};
getScreenHeight();
const requestDataAsid = computed(() => ({
  deleteIdList: selectedRows.value,
}));

const handleAudit = () => {
  if (selectedRows.value.length === 0) {
    alert("请勾选需要审核的数据！");
    return;
  }

  if (selectedRows.value[0].djStatus === "已审核") {
    alert("已经审核的数据无法再次审核！");
    return;
  }
  if (confirm("确认审核吗？") == false) {
    return;
  }
  // .then(() => {
  const userName = Cookies.get("userName");
  axios({
    url: "/audit/SingleRegistration",
    method: "post",
    data: {
      userName: userName,
      id: selectedRows.value.map((row) => row.id),
    },
  }).then((res) => {
    if (res.data.data == 200) {
      alert("审核成功！");
      // selectpurposes();
      searchButton();
      return;
    }
    ElMessage({
      showClose: true,
      message: "审核失败！",
      type: "error",
    });
  });

  // .catch(() => {});
};
const handleAudit0 = () => {
  if (selectedRows.value.length === 0) {
    alert("请勾选需要弃审的数据！");
    return;
  }

  if (selectedRows.value[0].djStatus === "未审核") {
    alert("已经弃审的数据无法再次弃审！");
    return;
  }
  if (confirm("确认弃审吗？") == false) {
    return;
  }
  // .then(() => {
  const userName = Cookies.get("userName");
  axios({
    url: "/audit/SingleRegistration0",
    method: "post",
    data: {
      userName: userName,
      id: selectedRows.value.map((row) => row.id),
    },
  }).then((res) => {
    if (res.data.data == 200) {
      alert("弃审成功！");
      // selectpurposes();
      searchButton();
      return;
    }
    ElMessage({
      showClose: true,
      message: "弃审失败！",
      type: "error",
    });
  });

  // .catch(() => {});
};
const deleteButton = async () => {
  if (selectedRows.value.length === 0) {
    alert("请勾选需要删除的数据！");
    return;

    // 在这里执行删除选中行的逻辑，可以使用 selectedRows.value[0] 获取唯一选中的一行
  }
  if (confirm("确认删除吗？") == false) {
    return;
  }
  // .then(() => {
  const userName = Cookies.get("userName");
  axios({
    url: "/del/SingleRegistration",
    method: "post",
    data: {
      id: selectedRows.value.map((row) => row.id),
    },
  }).then((res) => {
    if (res.data.data == 200) {
      alert("删除成功！");
      // selectpurposes();
      searchButton();
      return;
    }
    ElMessage({
      showClose: true,
      message: "删除失败",
      type: "error",
    });
  });

  // .catch(() => {});
};
const isDisabled = ref(true);
const gun = ref(true);
const dao = ref(true);
const getao = ref(true);
const gunXiang = ref(true);
const daoZhou = ref(true);
const clickOrganizationStatus = () => {
  form.categoryOfConsumables = categoryOfConsumables.value;
  if (form.categoryOfConsumables == 0) {
    gun.value = false;
    gunXiang.value = true;
    daoZhou.value = true;
    getao.value = true;
    dao.value = true;
    form.thicknessStandard = "";
    form.innerHoleStandard = "";
  }
  if (form.categoryOfConsumables == 1) {
    dao.value = false;
    gun.value = true;
    getao.value = true;
    gunXiang.value = true;
    daoZhou.value = true;
    form.width = "";
    form.height = "";
    form.taperStandard = "";
    form.taperLength = "";
    form.taperHeight = "";
  }
  if (form.categoryOfConsumables == 2) {
    getao.value = false;
    gun.value = true;
    dao.value = true;
    gunXiang.value = true;
    daoZhou.value = true;
    form.width = "";
    form.height = "";
    form.taperStandard = "";
    form.taperLength = "";
    form.taperHeight = "";
  }
  if (form.categoryOfConsumables == 3) {
    gunXiang.value = false;
    gun.value = true;
    getao.value = true;
    dao.value = true;
    daoZhou.value = true;
    form.diameter = "";
    form.workingFaceLength = "";
    form.workingFaceOuterDiameterStandard = "";
    form.nonWorkingFaceOuterDiameterStandard = "";
    form.innerHoleStandard = "";
    form.hardnessStandard = "";
    form.thicknessStandard = "";
    form.totalGrindingAmount = "";
    form.ljgrindingNumber = "";
    form.ljgrindingTimes = "";
    form.taperStandard = "";
    form.taperLength = "";
    form.taperHeight = "";
  }
  if (form.categoryOfConsumables == 4) {
    daoZhou.value = false;
    gun.value = true;
    getao.value = true;
    dao.value = true;
    gunXiang.value = true;
    form.width = "";
    form.height = "";
    form.workingFaceLength = "";
    form.innerHoleStandard = "";
    form.hardnessStandard = "";
    form.thicknessStandard = "";
    form.workingFaceOuterDiameterStandard = "";
    form.nonWorkingFaceOuterDiameterStandard = "";
    form.totalGrindingAmount = "";
    form.ljgrindingNumber = "";
    form.ljgrindingTimes = "";
    form.taperStandard = "";
    form.taperLength = "";
    form.taperHeight = "";
  }
};
const optionss = reactive([
  {
    value: "0",
    label: "辊轴类",
  },
  {
    value: "1",
    label: "刀具类",
  },
  {
    value: "2",
    label: "隔套类",
  },
  {
    value: "3",
    label: "辊箱",
  },
  {
    value: "4",
    label: "刀轴",
  },
  {
    value: "5",
    label: "其他",
  },
]);
const clickOrganizationIsdel = () => {
  form.djStatus = djStatus.value;
};
const options = reactive([
  {
    value: "0",
    label: "未审核",
  },

  {
    value: "1",
    label: "已审核",
  },
]);
// const clickOrganizationIsdel = () => {
//   form.djStatus = djStatus.value;
// };
// const options = reactive([
//   {
//     value: "0",
//     label: "未审核",
//   },

//   {
//     value: "1",
//     label: "已审核",
//   },
// ]);
// 修改弹窗

const handleRowClick = async (row, selection) => {
  dialogTableVisible1.value = true;
  showInsertButton.value = false;
  form.id = row.id;
  form.djNumber = row.djNumber;
  form.equipmentId = row.equipmentId;
  form.categoryOfConsumables = row.categoryOfConsumables;
  form.equipmentStructureId = row.equipmentStructureId;
  form.businessProjectId = row.businessProjectId;
  form.djDate = row.djDate;
  form.djStatus = row.djStatus;
  const userName = Cookies.get("userName");
  const currentFormattedDateTime = getCurrentFormattedDateTime();
  const currentFormattedDate = getCurrentFormattedDate();
  form.xgsj = currentFormattedDateTime;
  form.xgr = userName;
  // 回显测算类型
  // 回显子表
  axios({
    url: "/select/SingleRegistrationTableSub",
    method: "post",
    data: {
      id: form.id,
    },
  }).then((res) => {
    const testList = res.data;
    console.log(testList);
    if (testList.length === 0) {
      for (let a = 0; a < 21; a++) {
        addRow();
      }
    }
    total.value = res.data[0].total;
    // 根据 djzt 的值设置状态
    // zt: test.zt === 0 ? "待审" : "已审核",

    form.material = res.data[0].material;
    form.specification = res.data[0].id;

    form.specificationName = res.data[0].specification;
    form.model = res.data[0].id;
    form.modelName = res.data[0].model;
    form.color = res.data[0].color;
    form.closeStatus = res.data[0].closeStatus;
    form.workingFaceLength = res.data[0].workingFaceLength;
    form.allowManualScrap = res.data[0].allowManualScrap;
    form.weight = res.data[0].weight;
    form.length = res.data[0].length;
    form.width = res.data[0].width;
    form.height = res.data[0].height;
    form.diameter = res.data[0].diameter;
    form.thicknessStandard = res.data[0].thicknessStandard;
    form.innerHoleStandard = res.data[0].innerHoleStandard;
    form.hardnessStandard = res.data[0].hardnessStandard;
    form.workingFaceOuterDiameterStandard = res.data[0].workingFaceOuterDiameterStandard;
    form.nonWorkingFaceOuterDiameterStandard =
      res.data[0].nonWorkingFaceOuterDiameterStandard;
    form.totalGrindingAmount = res.data[0].totalGrindingAmount;
    form.ljgrindingNumber = res.data[0].ljgrindingNumber;
    form.ljgrindingTimes = res.data[0].ljgrindingTimes;
    form.residualGrindingCapacity = res.data[0].residualGrindingCapacity;
    form.workingFaceOuterDiameterStandard = res.data[0].workingFaceOuterDiameterStandard;
    form.supplierUniqueCode = res.data[0].supplierUniqueCode;
    form.wlbm = res.data[0].wlbm;
    form.partSingleId = res.data[0].partSingleId;
    form.roughnessStandard = res.data[0].roughnessStandard;
    form.taperStandard = res.data[0].taperStandard;
    form.taperLength = res.data[0].taperLength;
    form.taperHeight = res.data[0].taperHeight;
    form.quantity = res.data[0].quantity;
  });
};
// 新增页面展示
const openInseart = () => {
  OFFdialogTableVisible();
  dialogTableVisible1.value = true;
  showInsertButton.value = true;
};

interface RuleForm {
  id: string;
  djNumber: string;
  categoryOfConsumables: string;
  equipmentStructureId: string;
  businessProjectId: string;
  djDate: true;
  djStatus: string;
  partSingleId: string;
  taperStandard: string;
}
const form = reactive<RuleForm>({
  id: "",
  djNumber: "",
  color: "",
  categoryOfConsumables: "",
  equipmentStructureId: "",
  businessProjectId: "",
  equipmentId: "",
  quantity: "",
  djDate: "",
  djStatus: "0",
  mid: "",
  partSingleId: "",
  closeStatus: "",
  material: "",
  materialName: "", //材质名称
  specification: "",
  specificationName: "",
  model: "",
  modelName: "", //型号名称
  workingFaceLength: "",
  allowManualScrap: "",
  weight: "",
  length: "",
  width: "",
  height: "",
  diameter: "",
  thicknessStandard: "",
  innerHoleStandard: "",
  hardnessStandard: "",
  totalGrindingAmount: "",
  ljgrindingNumber: "",
  ljgrindingTimes: "",
  residualGrindingCapacity: "",
  wlbm: "",
  supplierUniqueCode: "",
  workingFaceOuterDiameterStandard: "",
  nonWorkingFaceOuterDiameterStandard: "",
  roughnessStandard: "",
  taperStandard: "",
  taperLength: "",
  taperHeight: "",
  inputType: "0",
  materialStatus: "0",
  circulationState: "0",
  lock: "0",
});
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<RuleForm>>({
  // name: [{ message: "请输入班组名称", trigger: "blur"  }],
});
// 子表展示
const cellStyle1 = ({ row, column, rowIndex, columnIndex }) => {
  return { borderColor: "#B9C9C7", padding: "0px", margin: 0 };
};
const showInsertButton = ref(true);
const multipleTableRef = ref();
const handleSelectionChange1 = (val: any[]) => {
  console.log("Selected Rows:", val);
};

const defaultRowData = {
  id: "",
  djNumber: "",
  categoryOfConsumables: "",
  equipmentStructureId: "",
  businessProjectId: "",
  djDate: "",
  djStatus: 0,
  mid: "",
  partSingleId: "",
  closeStatus: "",
  material: "",
  specification: "",
  model: "",
  workingFaceLength: "",
  allowManualScrap: "",
  weight: "",
  length: "",
  width: "",
  height: "",
  diameter: "",
  thicknessStandard: "",
  innerHoleStandard: "",
  hardnessStandard: "",
  totalGrindingAmount: "",
  ljgrindingNumber: "",
  ljgrindingTimes: "",
  residualGrindingCapacity: "",
  wlbm: "",
  supplierUniqueCode: "",
  workingFaceOuterDiameterStandard: "",
  nonWorkingFaceOuterDiameterStandard: "",
  roughnessStandard: "",
  taperStandard: "",
  taperLength: "",
  taperHeight: "",
};

const tableData1 = ref([
  {
    ...defaultRowData,
  },
  // More data...
]);
const OFFdialogTableVisible = () => {
  dialogTableVisible1.value = false;
  selectBusinessType();
  flushed();
};
const flushed = () => {
  Object.keys(form).forEach((key) => {
    form[key] = "";
  });
  form.inputType = "0";
  form.materialStatus = "0";
  form.circulationState = "0";
  form.lock = "0";
};
const addRow = () => {
  tableData1.value.push({ ...defaultRowData });
};
const requestFormTable = computed(() => {
  const usefulDataArray = tableData1.value.filter((item) => item.userId !== "");
  return {
    childList: usefulDataArray,
    ID: form.ID,
    name: form.name,
    businessTypeID: form.businessTypeID,
    BusinessTypeName: form.BusinessTypeName,
    BusinessTypeCode: form.BusinessTypeCode,
    itemType: form.itemType,
    businessProjectID: form.businessProjectID,
    description: form.description,
    status: form.status ? 1 : 0,
  };
});

const updateMaterialsFixed = () => {
  const userName = Cookies.get("userName");
  if (!userName) {
    router.push("/");
    return;
  }
  if (form.djStatus === "已审核") {
    alert("已经审核的数据无法再次修改！");
    return;
  }
  if (confirm("确认保存吗？") == false) {
    return;
  }

  // 显示确认弹窗
  axios({
    url: "/update/SingleRegistration/abc",
    method: "post",
    data: {
      id: form.id,
      djNumber: form.djNumber,
      categoryOfConsumables:
        form.categoryOfConsumables == "辊轴类"
          ? 0
          : form.categoryOfConsumables == "刀具类"
          ? 1
          : form.categoryOfConsumables == "隔套类"
          ? 2
          : form.categoryOfConsumables == "辊箱"
          ? 3
          : form.categoryOfConsumables == "刀轴"
          ? 4
          : 5,
      color: form.color,
      equipmentStructureId: form.equipmentStructureId,
      businessProjectId: form.businessProjectId,
      quantity: form.quantity,
      // djStatus: form.djStatus,
      djDate: form.djDate,
      partSingleId: form.partSingleId,
      closeStatus: form.closeStatus,
      material: form.material,
      specification: form.specification,
      model: form.model,
      workingFaceLength: form.workingFaceLength,
      allowManualScrap: form.allowManualScrap,
      weight: form.weight,
      length: form.length,
      width: form.width,
      height: form.height,
      diameter: form.diameter,
      thicknessStandard: form.thicknessStandard,
      innerHoleStandard: form.innerHoleStandard,
      hardnessStandard: form.hardnessStandard,
      totalGrindingAmount: form.totalGrindingAmount,
      ljgrindingNumber: form.ljgrindingNumber,
      ljgrindingTimes: form.ljgrindingTimes,
      residualGrindingCapacity: form.residualGrindingCapacity,
      wlbm: form.wlbm,
      supplierUniqueCode: form.supplierUniqueCode,
      workingFaceOuterDiameterStandard: form.workingFaceOuterDiameterStandard,
      nonWorkingFaceOuterDiameterStandard: form.nonWorkingFaceOuterDiameterStandard,
      userName: userName,
      roughnessStandard: form.roughnessStandard,
      taperStandard: form.taperStandard,
      taperLength: form.taperLength,
      taperHeight: form.taperHeight,
      inputType: form.inputType,
      materialStatus: form.materialStatus,
      circulationState: form.circulationState,
      lock: form.lock,
    },
  }).then((res) => {
    if (res.data.data == 200) {
      // location.reload();
      ElMessage({
        center: true,
        showClose: true,
        grouping: true,
        offset: window.screen.height / 9,
        message: "修改成功",
        type: "success",
      });
      OFFdialogTableVisible();
      searchButton();
      return;
    }
    ElMessage({
      center: true,
      showClose: true,
      grouping: true,
      offset: window.screen.height / 9,
      message: "修改失败",
      type: "error",
    });
  });
};

const insertMaterialsFixed = () => {
  const userName = Cookies.get("userName");
  //   if (form.equipmentStructureId == "") {
  //     alert("请输入设备结构id！");
  //     return 0;
  //   }
  if (form.partSingleId == "") {
    alert("请填写单件唯一码");
    return 0;
  }
  if (form.supplierUniqueCode == "") {
    alert("请填写供应商唯一码！");
    return 0;
  }
  if (form.wlbm == "") {
    alert("请选择物料编码！");
    return 0;
  }
  if (form.categoryOfConsumables == 0) {
    if (form.ljgrindingNumber == "") {
      alert("请输入累计磨削数量");
      return 0;
    }
    if (form.ljgrindingTimes == "") {
      alert("请输入累计磨削次数");
      return 0;
    }
    if (form.residualGrindingCapacity == "") {
      alert("请输入剩余磨削量");
      return 0;
    }
  }
  if (form.categoryOfConsumables == 1) {
    if (form.ljgrindingNumber == "") {
      alert("请输入累计磨削数量");
      return 0;
    }
    if (form.ljgrindingTimes == "") {
      alert("请输入累计磨削次数");
      return 0;
    }
    if (form.residualGrindingCapacity == "") {
      alert("请输入剩余磨削量");
      return 0;
    }
  }
  if (form.categoryOfConsumables == 2) {
    if (form.ljgrindingNumber == "") {
      alert("请输入累计磨削数量");
      return 0;
    }
    if (form.ljgrindingTimes == "") {
      alert("请输入累计磨削次数");
      return 0;
    }
    if (form.residualGrindingCapacity == "") {
      alert("请输入剩余磨削量");
      return 0;
    }
  }

  if (confirm("确认保存吗？") == false) {
    return;
  }
  const requestData = ref([]);

  axios({
    url: "/insert/SingleRegistration",
    method: "post",
    data: {
      id: form.id,
      djNumber: form.djNumber,
      color: form.color,
      categoryOfConsumables: form.categoryOfConsumables,
      equipmentStructureId: form.equipmentStructureId,
      businessProjectId: form.businessProjectId,
      equipmentId: form.equipmentId,
      quantity: form.quantity,
      djStatus: form.djStatus,
      djDate: form.djDate,
      partSingleId: form.partSingleId,
      closeStatus: form.closeStatus,
      material: form.material,
      specification: form.specification,
      model: form.model,
      workingFaceLength: form.workingFaceLength,
      allowManualScrap: form.allowManualScrap,
      weight: form.weight,
      length: form.length,
      width: form.width,
      height: form.height,
      diameter: form.diameter,
      thicknessStandard: form.thicknessStandard,
      innerHoleStandard: form.innerHoleStandard,
      hardnessStandard: form.hardnessStandard,
      totalGrindingAmount: form.totalGrindingAmount,
      ljgrindingNumber: form.ljgrindingNumber,
      ljgrindingTimes: form.ljgrindingTimes,
      residualGrindingCapacity: form.residualGrindingCapacity,
      wlbm: form.wlbm,
      supplierUniqueCode: form.supplierUniqueCode,
      workingFaceOuterDiameterStandard: form.workingFaceOuterDiameterStandard,
      nonWorkingFaceOuterDiameterStandard: form.nonWorkingFaceOuterDiameterStandard,
      userName: userName,
      roughnessStandard: form.roughnessStandard,
      taperStandard: form.taperStandard,
      taperLength: form.taperLength,
      taperHeight: form.taperHeight,
      inputType: form.inputType,
      materialStatus: form.materialStatus,
      circulationState: form.circulationState,
      lock: form.lock,
    },
  }).then((res) => {
    if (res.data.data == 200) {
      // location.reload();
      ElMessage({
        center: true,
        showClose: true,
        grouping: true,
        offset: window.screen.height / 9,
        message: "保存成功",
        type: "success",
      });
      OFFdialogTableVisible();

      return;
    } else if (res.data.msg == 505) {
      ElMessage({
        center: true,
        showClose: true,
        grouping: true,
        offset: window.screen.height / 9,
        message: "保存失败，不允许未经过审核就变更审核状态",
        type: "error",
      });

      return;
    } else if (res.data.msg == 501) {
      ElMessage({
        center: true,
        showClose: true,
        grouping: true,
        offset: window.screen.height / 9,
        message: "保存失败，不允许输入重复的单件唯一码",
        type: "error",
      });

      return;
    }
    ElMessage({
      center: true,
      showClose: true,
      grouping: true,
      offset: window.screen.height / 9,
      message: "保存失败",
      type: "error",
    });
  });

  return;
  console
    .log(formDataList.value)
    // 显示确认弹窗

    // ElMessageBox.confirm("确认保存吗？", "提示", {
    //   confirmButtonText: "确认",
    //   cancelButtonText: "取消",
    //   type: "warning",
    //   center: true,
    // })
    .then(() => {
      const requestData = formDataList.value.map((formData) => ({}));

      axios({
        url: "/insert/statusOperation",
        method: "post",
        data: {
          id: form.id,
          tableName: form.tableName,
          operationField: form.operationField,
          operationMenu: form.operationMenu,
          operationButton: form.operationMenu,
          precedingOperation: form.operationMenu,
          subsequentOperation: form.operationMenu,
          userName: userName,
          requestData: requestData.value,
        },
      }).then((res) => {
        if (res.data.data == 200) {
          // location.reload();
          ElMessage({
            center: true,
            showClose: true,
            grouping: true,
            offset: window.screen.height / 9,
            message: "保存成功",
            type: "success",
          });
          OFFdialogTableVisible();
          selectpurposes();

          return;
        }
        ElMessage({
          center: true,
          showClose: true,
          grouping: true,
          offset: window.screen.height / 9,
          message: "保存失败",
          type: "error",
        });
      });
    })
    .catch(() => {});
};
const getSelectedData = () => {
  const selectedRows = multipleTableRef.value.getSelectionRows();
};

const deleteSelectedRows = () => {
  const selectedRows = multipleTableRef.value.getSelectionRows();
  selectedRows.forEach((row) => {
    const index = tableData1.value.indexOf(row);
    if (index !== -1) {
      tableData1.value.splice(index, 1);
    }
  });
};

const deleteRow = (row: any) => {
  const index = tableData1.value.indexOf(row);
  if (index !== -1) {
    tableData1.value.splice(index, 1);
  }
};
const headerCellStyle1 = {
  borderColor: "#B9C9C7",
  backgroundColor: "#F2F4FA",
  fontWeight: "bold",
  color: "#1F2129",
  height: "40px",
  margin: 0,
  padding: 0,
};
const tableData3 = ref([]);
const tableData2 = ref([]);
var indexK3bm = 0;
const getK3vm = () => {
  axios({
    url: "/get/K3bm",
    method: "post",
    data: {
      page: page2.value,
      limit: limit2.value,
      k3bm: inputK3Bm.value,
      k3mc: inputK3Mc.value,
    },
  }).then((res) => {
    tableData3.value = res.data;
    if (res.data.length == 0) {
      total3.value == 0;
      return;
    }
    total3.value = res.data[0].total;
  });
};
const inputK3Bm = ref("");
const inputK3Mc = ref("");
const openWlbm = () => {
  dialogTableVisible4.value = true;
  getK3vm();
};
const handleSizeChange3 = (val: number) => {
  axios({
    url: "/get/K3bm",
    method: "post",
    data: {
      page: page2.value,
      limit: limit2.value,
      k3bm: inputK3Bm.value,
      k3mc: inputK3Mc.value,
    },
  }).then((res) => {
    tableData3.value = res.data;
    if (res.data.length == 0) {
      total3.value == 0;
      return;
    }
    total3.value = res.data[0].total;
  });
};
const handleCurrentChange3 = (val: number) => {
  axios({
    url: "/get/K3bm",
    method: "post",
    data: {
      page: page2.value,
      limit: limit2.value,
      k3bm: inputK3Bm.value,
      k3mc: inputK3Mc.value,
    },
  }).then((res) => {
    tableData3.value = res.data;
    if (res.data.length == 0) {
      total3.value == 0;
      return;
    }
    total3.value = res.data[0].total;
  });
};
const searc3Kh = () => {
  axios({
    url: "/get/K3bm",
    method: "post",
    data: {
      page: page2.value,
      limit: limit2.value,
      k3bm: inputK3Bm.value,
      k3mc: inputK3Mc.value,
    },
  }).then((res) => {
    tableData3.value = res.data;
    if (res.data.length == 0) {
      total3.value == 0;
      return;
    }
    total3.value = res.data[0].total;
  });
};

const updateK3 = () => {
  ElMessageBox.confirm("确认更新吗？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
    center: true,
  })
    .then(() => {
      axios({
        url: "/update/Jd/K3bm",
        method: "post",
        data: {},
      }).then((res) => {
        if (res.data.code == 200) {
          ElMessage({
            center: true,
            showClose: true,
            grouping: true,
            offset: window.screen.height / 9,
            message: res.data.message,
            type: "success",
          });
          searc3Kh();
          return;
        }
        ElMessage({
          center: true,
          showClose: true,
          grouping: true,
          offset: window.screen.height / 9,
          message: res.data.message,
          type: "error",
        });
      });
    })
    .catch(() => {});
};
const openBusiness = (index) => {
  dialogTableVisible2.value = true;
  geSysStatus();
};
const handleSizeChange2 = (val: number) => {
  geSysStatus();
};
const handleCurrentChange2 = (val: number) => {
  geSysStatus();
};
const selectedRows2 = ref([]);
const handleSelectionChange2 = (val) => {
  selectedRows2.value = val;
};
const geSysStatus = () => {
  axios({
    url: "/select/EquipmentMechanicalStructureMapping",
    method: "post",
    data: {
      ID: ID.value,
      page: page2.value,
      limit: limit2.value,
    },
  }).then((res) => {
    const statusList = res.data;
    tableData2.value = [];
    if (statusList.length == 0) {
      total2.value = 0;
      return;
    }
    total2.value = statusList[0].total;
    tableData2.value = statusList.map((test) => ({
      // 根据 djzt 的值设置状态
      // zt: test.zt === 0 ? "待审" : "已审核",
      autoId: test.autoId,
      ID: test.id,
      equipmentId: test.equipmentId,
      mechanicalStructureId: test.mechanicalStructureId,
      structureID: test.structureID,
      equipmentName: test.equipmentName,
      structureName: test.structureName,
      quantity: test.quantity,
      model: test.model,
      status: test.status,
      cid: test.cid,
    }));
  });
};
const RowClick2 = (row, selection) => {
  //   form.equipmentStructureId = row.mechanicalStructureId;
  //   form.equipmentId = row.equipmentId;
  //   form.quantity = row.quantity;
  form.modelName = row.model;
  form.model = row.cid;
  //   return;
  axios({
    url: "/select/ComponentBasicInformationTable",
    method: "post",
    data: {
      m0Id: row.mechanicalStructureId,
      equipmentStructureId: row.structureID,
      model: row.cid,
      page: page.value,
      limit: limit.value,
    },
  }).then((res) => {
    const testList = res.data;
    if (testList.length == 0) {
      loading.value = false;
      return;
    }
    total.value = res.data[0].total;
    // 根据 djzt 的值设置状态
    // zt: test.zt === 0 ? "待审" : "已审核",
    form.material = res.data[0].material;
    form.color = res.data[0].color;
    form.specification = res.data[0].id;
    form.specificationName = res.data[0].specification;
    form.closeStatus = res.data[0].closeStatus;
    form.businessProjectId = res.data[0].componentId;
    form.categoryOfConsumables = res.data[0].consumableCategory;
    form.workingFaceLength = res.data[0].workingFaceLength;
    form.allowManualScrap = res.data[0].allowManualScrap;
    form.weight = res.data[0].weight;
    form.length = res.data[0].length;
    form.width = res.data[0].width;
    form.height = res.data[0].height;
    form.diameter = res.data[0].diameter;
    form.thicknessStandard = res.data[0].thicknessStandard;
    form.innerHoleStandard = res.data[0].innerHoleStandard;
    form.hardnessStandard = res.data[0].hardnessStandard;
    form.workingFaceOuterDiameterStandard = res.data[0].workingFaceOuterDiameterStandard;
    form.nonWorkingFaceOuterDiameterStandard =
      res.data[0].nonWorkingFaceOuterDiameterStandard;
    form.totalGrindingAmount = res.data[0].grindingTotal;
    form.workingFaceOuterDiameterStandard = res.data[0].workingFaceOuterDiameterStandard;
    form.roughnessStandard = res.data[0].roughnessStandard;
    form.taperStandard = res.data[0].taperStandard;
    form.taperLength = res.data[0].taperLength;
    form.taperHeight = res.data[0].taperHeight;
  });
  dialogTableVisible2.value = false;
};
const RowClick3 = (row) => {
  form.wlbm = row.fnumber;
  dialogTableVisible4.value = false;
};
// 弹窗
const indexEmployeeJob = ref(0);

const flushed1 = () => {
  form.value = [];
  for (let a = 0; a < 21; a++) {
    addRow();
  }
};
const inputTypeTrue = ref(true);
const inputTypeChange = () => {
  if (form.inputType == "0") {
    form.materialStatus = "0";
    form.circulationState = "0";
    form.lock = "0";
    inputTypeTrue.value = true;
  } else {
    inputTypeTrue.value = false;
  }
};
</script>
<style scoped>
.el-table {
  /* --el-table-row-hover-bg-color: rgba(0, 158, 250, 0.219) !important; */
  /* --el-table-row-hover-bg-color: #fef5f0 !important; */
  --el-table-row-hover-bg-color: rgba(254, 245, 240, 0.8) !important;
}

/* 选中 */
.el-table {
  /* --el-table-row-hover-bg-color: #fef5f0 !important; */
  --el-table-row-hover-bg-color: rgba(254, 245, 240, 0.8) !important;
}

.down-tree ::v-deep .el-tree-node.is-expanded > .el-tree-node__children {
  display: inline;
}

.down-tree {
  height: 100%;
}

.demo-pagination-block {
  float: right;
}

.custom-dialog {
  position: fixed;
  top: 10em;
  left: 50%;
  width: 20%;
  /* 其他样式... */
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.labs {
  margin-bottom: 0px;
}

.lab {
  background-color: rgba(247, 247, 247, 0.903);
  /* border-radius: 5px; */
  text-align: center;
  margin-bottom: 0px;
  text-align: right;
}
</style>
